<?php
//创建websocket服务器对象，监听0.0.0.0:9502端口
$ws = new swoole_websocket_server("0.0.0.0", 9503);

//监听WebSocket连接打开事件
$ws->on('open', 'onOpen');
function onOpen($ws, $request)
{
    echo '会话id：' . print_r($request->fd, true) . PHP_EOL;
    echo '请求参数：' . print_r($request->get, true) . PHP_EOL;
    echo '服务信息：' . print_r($request->server, true) . PHP_EOL;

    $ws->push($request->fd, "欢迎来到聊天室");
}

//监听WebSocket消息事件
$ws->on('message', function ($ws, $frame) {
    echo "Message: {$frame->data}\n";
    $ws->push($frame->fd, "server: {$frame->data}");
});

//监听WebSocket连接关闭事件
$ws->on('close', function ($ws, $fd) {
    echo "client-{$fd} is closed\n";
});

$ws->start();